package com.forum.module.system.service.mail;

import cn.hutool.core.util.StrUtil;
import com.forum.framework.common.exception.util.ServiceExceptionUtils;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import static com.forum.module.system.enums.ErrorCodeConstants.*;

/**
 * 功能:
 * 作者:ouyzh
 * 日期:2025/3/18 21:50
 */
@Service
public class MailServiceImpl implements MailService {

    private static final Logger log = LoggerFactory.getLogger(MailServiceImpl.class);

    @Value("${spring.mail.username}")
    private String clientMail;
    private final JavaMailSender mailSender;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    public MailServiceImpl(JavaMailSender mailSender) {
        this.mailSender = mailSender;
    }

    @Override
    public boolean sendMail(String email, String code) {
        if (StrUtil.isEmpty(email)) {
            return false;
        }

        String subject = "🌟 您的邮箱验证码";

        String text = "<html>"
                + "<head><style>"
                + "body { font-family: 'Arial', sans-serif; font-size: 14px; color: #333; line-height: 1.6; }"
                + "h2 { color: #4CAF50; font-size: 20px; text-align: center; }"
                + ".code { font-size: 24px; font-weight: bold; color: #ff5722; text-align: center; display: block; padding: 10px 0; font-family: 'Courier New', monospace; }"
                + ".button { display: inline-block; padding: 12px 24px; background-color: #4CAF50; color: #fff; text-decoration: none; border-radius: 5px; text-align: center; margin-top: 20px; }"
                + ".footer { font-size: 12px; color: #888; text-align: center; margin-top: 20px; }"
                + "</style></head>"
                + "<body>"
                + "<h2>亲爱的用户，您好！🎉</h2>"
                + "<p>您正在请求邮箱验证码，请查收以下内容：</p>"
                + "<div class='code'>🔑 " + code + " 🔑</div>"
                + "<p>验证码有效期为5分钟，请尽快使用。⏳</p>"
                + "<a href='#' class='button'>立即验证</a>"
                + "<p class='footer'>此邮件由系统自动发出，工作人员不会索取验证码，请勿泄露。🛑</p>"
                + "</body>"
                + "</html>";

        SimpleMailMessage msg = new SimpleMailMessage();

        msg.setFrom(clientMail);
        msg.setTo(email);
        msg.setSubject(subject);
        msg.setText(text);
        try {
            mailSender.send(msg);
            log.info("msg=======>{}", msg);
        } catch (MailException e) {
            throw ServiceExceptionUtils.exception(GET_MAIL_CODE_ERROR);
        }
        return true;
    }

    @Override
    public boolean validMail(String email, String code) {
        String validCode = (String) redisTemplate.opsForValue().get(email);
        if (StrUtil.isBlank(validCode)) {
            throw ServiceExceptionUtils.exception(CODE_TIME_OUT);
        }
        validCode = validCode.replaceAll("^\"|\"$", "");
        return code.equals(validCode);
    }
}
